if (!$power_manager.isIgnoringBatteryOptimizations()) {
    console.log("未开启忽略电池优化");
    $power_manager.requestIgnoreBatteryOptimizations();
}


while (true) {
    var now_time = new Date();
    var now_hours = now_time.getHours();
    var now_min = now_time.getMinutes();
    if (now_hours === 8 && now_min >= 50 || now_hours === 9 && now_min <= 10) {
        do_main();
    }else if (now_hours === 21 && now_min >= 30 || now_hours === 22 && now_min <= 30){
        do_main();
    }
    else {
        // 1-10分钟
        sleep(random(1,10) * 60 * 1000);
    }

}

//主程序
function do_main() {
    var now_time = new Date();
    var now_hours = now_time.getHours();
    auto.waitFor("fast"); //检查无障碍权限启动
    bright_screen();
    sleep(500);
    unlock_screen();//只支持安卓>=7.0
    sleep(500);
    stop_app();
    sleep(500);
    is_login();
    sleep(500);
    in_kaoqin();
    sleep(500);
    if (now_hours <= 9) {
        do_clock_in();
        sleep(500);
    } else {
        do_clock_out();
        sleep(500);
    }
    device.cancelKeepingAwake();
    log("主程序执行完毕，取消设备常亮");
}

//子程序：
//1.亮屏//    bright_screen()
//2.解锁//unlock_screen()
//3.结束钉钉//stop_app()
//4.是否登录//is_login()
//5.进入考勤页面//in_kaoqin()
//6.上班打卡//do_clock_in()
//7.下班打卡//do_clock_out()


//1.亮屏
function bright_screen() {
    log("进入亮屏子程序");
    device.wakeUpIfNeeded(); //唤醒设备
    device.keepScreenOn(); //保持亮屏
    log("已唤醒");
    if (!device.isScreenOn()) {
        log("未唤醒");
        device.wakeUpIfNeeded();
        bright_screen();
    }
}

//2.解锁--安卓7.0及以上才能支持手势及坐标操作
function unlock_screen() {
    log("进入解锁子程序");
    swipe(500,2000,500,1000,210); //上划调出手势界面
    sleep(500);
    var password = "123456"  //这里输入你手机的密码
    for(var i = 0; i < password.length; i++)
    {
        var p = text(password[i].toString()).findOne().bounds();
        click(p.centerX(), p.centerY());
        sleep(100);
    }
    sleep(500);
    home();
    sleep(500);
}

//3.结束钉钉进程，确保启动钉钉后进入主页面
function stop_app() {
    log("进入结束钉钉进程子程序");
    app.openAppSetting("com.alibaba.android.rimet"); //进入钉钉设置
    text(app.getAppName("com.alibaba.android.rimet")).waitFor();
    let is_sure = textMatches(/(强制.*|.*停止|强行.*)/).clickable(true).findOne();
    if (is_sure.enabled()) {
        sleep(1000);
        textMatches(/(强制.*|.*停止|.*确定.*)/).clickable(true).findOne().click();
        sleep(1000);
        textMatches(/(强制.*|.*停止|.*确定.*)/).clickable(true).findOne().click();
        log(app.getAppName("com.alibaba.android.rimet") + "应用已被关闭");
        sleep(500);
        home();
    } else {
        log(app.getAppName("com.alibaba.android.rimet") + "应用不能被正常关闭,重试");
        sleep(500);
        home();
    }
}

//4.判断是否未登录   部分需要手动修改代码
function is_login() {
    log("进入登录判定子程序");
    app.launchPackage("com.alibaba.android.rimet");
    sleep(5000);
    //判定是否在登录页面
    if (id("et_pwd_login").exists()) {
        var phone = id("et_phone_input").findOne();
        phone.setText("15530192694");
        var pwd = id("et_pwd_login").findOne();
        sleep(1000);
        pwd.setText("ss970214");
        id("et")
        id("btn_next").findOne().click();
        info = "账号未登录>>已登录成功";
        log(info)
    } else {
        if (className("android.widget.RelativeLayout").exists()) {
            log("账号已登录");
            sleep(500);
        } else {
            log("未检测到钉钉活动页面>>重启钉钉");
            is_login();
        }
    }
}

//5.进入考勤页面
function in_kaoqin() {
    log("进入考勤页面切换子程序");
    if (null != textMatches("工作台").clickable(true).findOne(3000)) {
        toast("text中找到工作台按钮");
        anniu_gongzou = textMatches(/(.*工作台.*)/).findOnce();
        log("text中找到工作台按钮");
        sleep(500);
        anniu_gongzou.click();
        sleep(6000);
        if (null != textMatches("考勤打卡").clickable(true).findOne(3000)) {
            toast("text中找到考勤打卡按钮");
            anniu_kaoqin = textMatches(/(.*考勤打卡.*)/).findOne();
            log("text中找到考勤按钮");
            sleep(500);
            anniu_kaoqin.click();
            sleep(5000);
            log("进入打卡页面");
        } else {
            if (null != descMatches("考勤打卡").clickable(true).findOne(3000)) {
                toast("desc中找到考勤打卡按钮");
                anniu_kaoqin = descMatches(/(.*考勤打卡.*)/).findOne();
                log("desc中找到考勤按钮");
                sleep(500);
                anniu_kaoqin.click();
                sleep(5000);
                log("进入打卡页面");
            } else {
                sleep(500);
                log("未进入打卡页面");
                sleep(500);
                in_kaoqin();
            }
        }
    }
    if (null != descMatches("工作台").clickable(true).findOne(3000)) {
        toast("desc中找到工作台按钮");
        anniu_gongzou = descMatches(/(.*工作台.*)/).findOnce();
        log("desc中找到工作台按钮");
        sleep(500);
        anniu_gongzou.click();
        sleep(6000);
        if (null != textMatches("考勤打卡").clickable(true).findOne(3000)) {
            toast("text中找到考勤打卡按钮");
            anniu_kaoqin = textMatches(/(.*考勤打卡.*)/).findOne();
            log("text中找到考勤按钮");
            sleep(500);
            anniu_kaoqin.click();
            sleep(5000);
            log("进入打卡页面");
        } else {
            if (null != descMatches("考勤打卡").clickable(true).findOne(3000)) {
                toast("desc中找到考勤打卡按钮");
                anniu_kaoqin = descMatches(/(.*考勤打卡.*)/).findOne();
                log("desc中找到考勤按钮");
                sleep(500);
                anniu_kaoqin.click();
                sleep(5000);
                log("进入打卡页面");
            } else {
                sleep(500);
                log("未进入打卡页面");
                sleep(500);
                in_kaoqin();
            }
        }
    }
}

//6.上班打卡
function do_clock_in() {
    log("进入上班打卡子程序");
    if (null != descMatches("上班打卡").findOne(1000)) {
        toast("desc中找到上班打卡按钮")
        anniu_click_in = descMatches(/(.*上班打卡.*)/).findOnce();
        log("desc中找到上班打卡按钮");
        sleep(1000);
        textContains("已进入考勤范围").waitFor();
        var randomtime = random(0, 3) * 60 * 1000;
        log(randomtime / 1000 + "秒后执行");
        toast(randomtime / 1000 + "秒后执行");
        sleep(randomtime);
        do {
            anniu_click_out.click();
            sleep(100);
            anniu_click_out.parent().click()
        }
        while (text("我知道了").waitFor());
        sleep(1000);
        if (null != descMatches("我知道了").findOne(1000)) {
            desc("我知道了").findOne().click();
        }
        if (null != textMatches("我知道了").clickable(true).findOne(1000)) {
            text("我知道了").findOne().click();
        }
        sleep(2000);
        log("按下打卡按钮，打卡成功");
    } else {
        if (null != textMatches("上班打卡").clickable(true).findOne(1000)) {
            toast("text中找到上班打卡按钮");
            anniu_click_in = textMatches(/(.*上班打卡.*)/).findOnce();
            log("text中找到上班打卡按钮");
            sleep(1000);
            textContains("已进入考勤范围").waitFor();
            var randomtime = random(0, 3) * 60 * 1000;
            log(randomtime / 1000 + "秒后执行");
            toast(randomtime / 1000 + "秒后执行");
            sleep(randomtime);
            do {
                anniu_click_out.click();
                sleep(100);
                anniu_click_out.parent().click()
            }
            while (text("我知道了").waitFor());
            if (null != descMatches("我知道了").clickable(true).findOne(1000)) {
                desc("我知道了").findOne().click();
            }
            if (null != textMatches("我知道了").clickable(true).findOne(1000)) {
                text("我知道了").findOne().click();
            }
            sleep(2000);
            log("按下打卡按钮，打卡成功");
        } else {
            log("未找到上班打卡按钮>>继续找");
            sleep(500);
            let daka_ok = textMatches(/(.*下班打.*|下班打.*)/).findOnce();
            if (daka_ok != null) {
                log("已出现下班打卡按钮，无需再按");
                sleep(1000);
                home();
            } else {
                log("未打卡，重试");
                do_clock_in();
            }
        }
    }
    home();
    sleep(1000);
    log("脚本已执行完毕");
}

//7.下班打卡
function do_clock_out() {
    log("进入下班打卡子程序");
    if (null != descMatches("下班打卡").findOne(1000)) {
        toast("desc中找到下班打卡按钮")
        anniu_click_out = descMatches(/(.*下班打卡.*)/).findOnce();
        log("desc中找到下班打卡按钮");
        sleep(1000);
        textContains("已进入考勤范围").waitFor();
        var randomtime = random(0, 3) * 60 * 1000;
        log(randomtime / 1000 + "秒后执行");
        toast(randomtime / 1000 + "秒后执行");
        sleep(randomtime);
        anniu_click_out.click();
        sleep(1000);
        if (null != descMatches("我知道了").clickable(true).findOne(1000)) {
            desc("我知道了").findOne().click();
        }
        if (null != textMatches("我知道了").clickable(true).findOne(1000)) {
            text("我知道了").findOne().click();
        }
        sleep(2000);
        log("按下打卡按钮，打卡成功");
    } else {
        if (null != textMatches("下班打卡").findOne(1000)) {
            toast("text中找到下班打卡按钮")
            anniu_click_out = textMatches(/(.*下班打卡.*)/).findOnce()
            log("text中找到下班打卡按钮");
            sleep(1000);
            textContains("已进入考勤范围").waitFor();
            var randomtime = random(0, 3) * 60 * 1000;
            log(randomtime / 1000 + "秒后执行");
            toast(randomtime / 1000 + "秒后执行");
            sleep(randomtime);
            anniu_click_out.click();
            if (null != descMatches("我知道了").clickable(true).findOne(1000)) {
                desc("我知道了").findOne().click();
            }
            if (null != textMatches("我知道了").clickable(true).findOne(1000)) {
                text("我知道了").findOne().click();
            }
            sleep(2000);
            log("按下打卡按钮，打卡成功");
        } else {
            log("未找到下班打卡按钮>>继续找");
            sleep(500);
            let daka_ok = textMatches(/(.*新打卡.*|更新打.*)/).findOnce()
            if (daka_ok != null) {
                log("已出现更新打卡按钮，打卡成功")
                sleep(1000);
                home();
            } else {
                log("未打卡，重试");
                do_clock_out();
            }
        }
    }
    home();
    sleep(1000);
    log("脚本已执行完毕");
}


function random(min, max) {

    return Math.floor(Math.random() * (max - min)) + min;

}